package org.hit.burkun.network;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;

import org.hit.burkun.db.DBHelper;
import org.hit.burkun.umls.relation.UniformRelation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class StaticMethods {
	private static String querySql = "SELECT ent1_dbid, ent1_umls, ent2_dbid,ent2_umls,scores,is_direct,rel_detials,ent1_type, ent2_type "
			+ "FROM merge_birelation";
	//融合umls，将同样的umls，变成一个。不考虑多个umls的映射
	private static Logger log = LoggerFactory.getLogger(StaticMethods.class);
	
	private static void initAndSaveGraph(String path, boolean useDirect){
		int edgeCounter1 = 0;
		int edgeCounter2 = 0;
		log.debug("load all data from merge relation table...");
		HashMap<String, Vertex> umlsToVertex = new HashMap<>();
		SerializableGraph sg = new SerializableGraph();
		DBHelper dbh = DBHelper.getLocalBioData();
		dbh.connectionDB();
		Collection<String[]> res = dbh.queryData(querySql, 9);
		dbh.disconnection();
		log.debug("init graph...");
		//init vertex
		for(String[] line : res){
			String umls1 = line[1];
			String umls2 = line[3];
			//这里需要做融合了，截取所有的umls然后判断是不是在里面，保存小的umls即可
			String mapUmls1 = getMapUmls(umls1, umlsToVertex);
			String mapUmls2 = getMapUmls(umls2, umlsToVertex);
			if(umlsToVertex.containsKey(mapUmls1)){
				Vertex v = umlsToVertex.get(mapUmls1);
				v.addDbId(line[0]);
				v.setNodeType(line[7]);
			}else{
				Vertex v = new Vertex();
				v.setUmlsId(mapUmls1);
				v.addDbId(line[0]);
				v.setNodeType(line[7]);
				umlsToVertex.put(mapUmls1, v);
			}
			if(umlsToVertex.containsKey(mapUmls2)){
				Vertex v = umlsToVertex.get(mapUmls2);
				v.addDbId(line[2]);
				v.setNodeType(line[8]);
			}else{
				Vertex v = new Vertex();
				v.setUmlsId(mapUmls2);
				v.addDbId(line[2]);
				v.setNodeType(line[8]);
				umlsToVertex.put(mapUmls2, v);
			}
			Vertex v1 = umlsToVertex.get(mapUmls1);
			Vertex v2 = umlsToVertex.get(mapUmls2);
			if(line[5].equals("0")){
				if(line[6].equals(UniformRelation.TYPE.STRING_GENE.getTag())){	
					EdgeInfo ei1 = new EdgeInfo(v2,  line[6]);
					ei1.setOrgScore(Double.parseDouble(line[4]));
					sg.addEdge(v1, ei1);
					edgeCounter2 += 1;
				}else{
					EdgeInfo ei1 = new EdgeInfo(v2,  line[6]);
					EdgeInfo ei2 = new EdgeInfo(v1,  line[6]);
					sg.addEdge(v1, ei1);
					sg.addEdge(v2, ei2);
					edgeCounter2 += 2;
				}
				
			}else{
				if(useDirect){
					//假设ontology只往一个方向跑
					EdgeInfo ei1 = new EdgeInfo(v2,  line[6]);
					sg.addEdge(v1, ei1);
					edgeCounter1 += 1;
				}else{
					if(line[6].equals(UniformRelation.TYPE.STRING_GENE.getTag())){	
						EdgeInfo ei1 = new EdgeInfo(v2,  line[6]);
						ei1.setOrgScore(Double.parseDouble(line[4]));
						sg.addEdge(v1, ei1);
						edgeCounter2 += 1;
					}else{
						EdgeInfo ei1 = new EdgeInfo(v2, line[6]);
						EdgeInfo ei2 = new EdgeInfo(v1, line[6]);
						sg.addEdge(v1, ei1);
						sg.addEdge(v2, ei2);
						edgeCounter2 += 2;
					}
				}
			}
		}
		sg.setAllVertexs(umlsToVertex);
		try {
			log.debug("saving graph....");
			SerializableGraph.writeObject(new File(path), sg);
			log.debug("graph saved!");
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println(edgeCounter1 +","+ edgeCounter2);
	}
	
	//获取最小的umls
	private static String getMapUmls(String umls, HashMap<String, Vertex> umlsToVertex){
		String[] us = umls.split(",");
		for(String u : us){
			if(umlsToVertex.containsKey(u)){
				return u;
			}
		}
		Arrays.sort(us);
		return us[0];
	}
	public static void main(String[] args){
		initAndSaveGraph("data/graphDir.obj", true);
		initAndSaveGraph("data/graphNoDir.obj", false);
	}
	

}
